/**
 * @author rxliuli
 */
const {
    arrayToMap
} = rx
const app = new BasicTableVue({
    el: '#app',
    data() {
        return {
            api: sysUserApi,
            columns: [
                new TableColumn({prop: 'id', title: '编号'}),
                new TableColumn({prop: 'regMail', title: '用户名', customComponent: true}),
                new TableColumn({prop: 'loginName', title: '姓名'}),
                new TableColumn({
                    prop: 'sysUserRoleList',
                    title: '管理员组',
                    customComponent: true,
                }),
                new TableColumn({prop: 'createTime', title: '创建时间', formatter: TableColumn.datetimeFormat}),
                new TableColumn({prop: 'lastLoginTime', title: '上次登录', formatter: TableColumn.datetimeFormat}),
                new TableColumn({prop: 'loginSource', title: '登录来源'}),
                new TableColumn({
                    prop: 'status',
                    title: '状态',
                    formatter: fieldFormatter.facade(fieldFormatter.sysUser.statusMap),
                }),
                new TableColumn({prop: 'operating', title: '操作', customComponent: true}),
            ],
            userTypeMap: new Map(),
            sysRoleList: [],
            sysRoleMap: new Map(),
        }
    },
    methods: {
        createForm() {
            return new SysUser()
        },
        updateSysUser(sysUser) {
            forward('/system-config/modify/admin_privilege_admin_edit', sysUser)
        },
        async deleteSysUser(id) {
            const confirmResult = await rxPrompt.confirm('确定要删除么？', ['确定', '取消'])
            if (!confirmResult) {
                return
            }
            const deleteResult = await ajaxOption({
                res: this.api.delete(id),
                danger: '删除失败',
                success: '删除成功, 即将重新加载',
            })
            if (deleteResult) {
                await this.searchPage()
            }
        },
        async sysRoleListInit() {
            this.sysRoleList = await sysRoleApi.listForAll()
            this.sysRoleMap = arrayToMap(this.sysRoleList, ({id}) => id, ({name}) => name)
        },
        async init() {
            this.sysRoleListInit()
            this.userTypeMap = await sysRoleApi.listOfMap()
        },
    },
    computed: {
        fieldFormatter() {
            return fieldFormatter
        },
    },
})